package cn.kgc.vue.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.form.TemplateLsitForm;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Template;
import cn.kgc.vue.service.TemplateService;
import cn.kgc.vue.mapper.TemplateMapper;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author 
* @description 针对表【template】的数据库操作Service实现
* @createDate 2023-08-20 18:34:35
*/
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template>
    implements TemplateService{
  @Autowired
  private TemplateMapper templateMapper;
    @Override
    public ResponseResult getlist(TemplateLsitForm templateLsitForm) {
      ResponseResult responseResult = new ResponseResult();
      Page<Template> page = new Page<>(templateLsitForm.getPage(), templateLsitForm.getLimit());
      LambdaQueryWrapper<Template> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(ObjectUtils.isNotEmpty(templateLsitForm.getFileName()),Template::getFileName,templateLsitForm.getFileName());
      Page<Template> templatePage = templateMapper.selectPage(page, wrapper);
      List<Template> records = templatePage.getRecords();
      long total = templatePage.getTotal();
      HashMap<String, Object> map = new HashMap<>();
      map.put("template",records);
      map.put("total",total);
      responseResult.success(ResponseStatus.SUCCESS,map);
      return responseResult;
    }

  @Override
  public ResponseResult upload(MultipartFile file) {
    ResponseResult responseResult = new ResponseResult();
    if(file.isEmpty()){
      responseResult.fail();
      return responseResult;
    }
    //文件名
    String originalFilename = file.getOriginalFilename();
    //取到后缀名
    String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
    String uuid = IdUtil.simpleUUID();
    String uuidName=uuid+"."+filenameExtension;
    File file1 = new File("D:\\upload\\" + uuidName);
    if (!file1.exists()){
      file1.mkdirs();
    }
    try {
      file.transferTo(file1);
      Template template = new Template();
      template.setUuidName(uuidName);
      template.setFileName(StringUtils.stripFilenameExtension(originalFilename));
      template.setCreateTime(new Date());
      double size = file.getSize() / 1024;
      template.setSize(size);
      templateMapper.insert(template);
      responseResult.success(ResponseStatus.ADD_SUCCESS);
      return  responseResult;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public void down(HttpServletResponse response,@RequestParam("uuidname") String uuidname) {
    try {
      FileInputStream fileInputStream = new FileInputStream("D:\\upload\\" + uuidname);
      response.setContentType("application/force-download");// 设置强制下载不打开
      response.addHeader("Content-Disposition", "attachment;fileName=" +uuidname );// 设置文件名
      ServletOutputStream outputStream = response.getOutputStream();
      IOUtils.copy(fileInputStream, outputStream);
      fileInputStream.close();
      outputStream.close();
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}




